****************************************************************************
use "$datapath/Portfolio_Paper_zbar.dta", clear // average country shares

merge m:1 datem country  using "$datapath/confidence" , nogenerate // merge with confidence variables

* sample selection
keep if datem >= ym(2002,1) & datem <= ym(2016,7)
drop if mi(fundid)
*****************************************************************************

*****************************************************************************
* BENCHMARK INSTRUMENTS
keep datem fundid country zbar* cci cli bci dcci dcli dbci // levels and first difference of consumer confidence index, business confidence index and leading indicator
*****************************************************************************

*****************************************************************************
tempfile master
save `master', replace

*****************************************************************************
*** fund weighted us differential
levelsof country, local(country)
foreach c of local country {
	*** main
	qui gen s_cci_`c' = zbar_`c'*cci
	
	qui gen s_cli_`c' = zbar_`c'*cli
	
	qui gen s_bci_`c' = zbar_`c'*bci
	
	qui gen s_dcci_`c' = zbar_`c'*dcci
	
	qui gen s_dcli_`c' = zbar_`c'*dcli
	
	qui gen s_dbci_`c' = zbar_`c'*dbci
}
*****************************************************************************

*****************************************************************************
* reduce information and reshape the data
qui collapse (sum) s_* , by(fundid datem)

foreach v of varlist s_* {
	qui replace `v' = . if `v' == 0
}

global singlesum "s_cci_ s_bci_ s_cli_ s_dcci_ s_dbci_ s_dcli_ "

qui reshape long $singlesum , i(datem fundid) j(country) string 
drop if mi(s_bci_)
*****************************************************************************

*****************************************************************************
* import us differentials
merge m:1 datem country  using "$datapath/confidence" , nogenerate
*****************************************************************************

* main
gen z_bci = bci - s_bci_

gen z_cci = cci - s_cci_

gen z_cli = cli - s_cli_

gen z_dbci = dbci - s_dbci_

gen z_dcci = dcci - s_dcci_

gen z_dcli = dcli - s_dcli_

keep datem fundid country z_* 

sort fundid country datem

save "$datapath/Portfolio_Paper_Confidence_Table_B3.dta", replace
* end
*****************************************************************************